package webflux.reactor;

import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2021-05-18 18:16:00
 */
public class ReactorDemo {

    @Test
    public void test01(){
        //0~1个序列
        Mono.empty().subscribe(System.out::println);
        Mono.just("hello lu").subscribe(System.out::println);

        //0～n个序列
        Flux.just(1,2,3,4,5,6).subscribe(System.out::print);
        Flux.fromIterable(Arrays.asList("a", "b", "c")).subscribe(System.out::print);
        Flux.fromArray(new String[]{"a1", "b2", "c3"}).subscribe(System.out::print);
        Flux.fromStream(Stream.of(new String[]{"A1", "B2", "C3"})).subscribe(System.out::print);
        Flux.range(1, 3).subscribe(System.out::print);
        /*
            2*0=0
            2*1=2
            ...
            2*9=18
         */
        Flux.generate(() -> 0, (i, sink) -> {
            sink.next("2*" + i + "=" + 2 * i);
            if(i == 9){
                sink.complete();
            }
            return i + 1;
        }).subscribe(System.out::println);
    }

    @Test
    public void test02(){
        String src = "asdj aksjdh wiue aspod xmaosd";
        Flux.fromArray(src.split(" "))
                .flatMap(i -> Flux.fromArray(i.split("")))
                .distinct()
                .sort()
                .subscribe(System.out::print);
    }




}
